package com.quanshu.change.person.view;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.quanshu.change.person.adapter.LimitedGoodsAdapter;
import com.quanshu.change.person.entity.SeckillGoodsBuyEntity;
import com.quanshu.exchange.R;
import com.quanshu.exchange.support.api.ShopInterface;
import com.quanshu.exchange.support.net.BaseResponse;
import com.quanshu.exchange.support.net.core.BaseObserver;
import com.quanshu.exchange.support.net.core.BaseTransformer;
import com.quanshu.exchange.support.net.core.NetClient;
import com.quanshu.exchange.support.utils.LogUtil;
import com.quanshu.exchange.support.utils.RxViewHelper;
import com.quanshu.exchange.ui.base.BaseActivity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class LimitedGoodsActivity extends BaseActivity {

    @BindView(R.id.back)
    LinearLayout back;
    @BindView(R.id.title)
    RelativeLayout title;
    @BindView(R.id.rl)
    RecyclerView rl;
    private LimitedGoodsAdapter limitedGoodsAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_limited_goods);
        ButterKnife.bind(this);
        initView();
        getData();
    }

    private void initView() {
        RxViewHelper.getInstance().clicks(back, aVoid -> {
            finish();
        });
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rl.setLayoutManager(linearLayoutManager);
    }

    //获取限购商品购买明细
    private void getData() {
        if (currentUser != null) {
            NetClient.getInstance().getShopService(ShopInterface.class)
                    .getHotGoods(HotGoodsActivity.HOT_LIMITE_ISINN, currentUser.getMemberid() + "", HotGoodsActivity.HOT_LIMITE_TYPE, HotGoodsActivity.HOT_LIMITE_ISTYPE)
                    .compose(new BaseTransformer<>(this,true,false))
                    .subscribe(new BaseObserver<BaseResponse<List<SeckillGoodsBuyEntity>>>() {
                        @Override
                        public void onSuccess(BaseResponse<List<SeckillGoodsBuyEntity>> response) {
                            if (response.getCode() == 1) {
                                limitedGoodsAdapter = new LimitedGoodsAdapter(response.getData());
                                rl.setAdapter(limitedGoodsAdapter);
                            }
                        }

                        @Override
                        public void onFailure(Throwable e) {
                            LogUtil.printException(e);
                        }
                    });
        }
    }
}
